﻿using System;
using System.Collections.Generic;
using EPiServer.Core;
using EPiServer.DataAccess;
using EPiSugar.UnitTesting;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using EPiServer;
using EPiServer.Security;

namespace Suggestions
{
    [TestFixture]
    public class SubmittingSuggestions
    {
        private const int SuggestionBoxPageTypeID = 10;
        private PageReference boxLink;

        [SetUp]
        public void SetUp()
        {
            Fakes.Initialize(new FakeStructureBuilder(1, 0));

            PageData box = DataFactory.Instance.GetDefaultPageData(PageReference.StartPage, SuggestionBoxPageTypeID);
            box.PageName = "Suggestion box";
            boxLink = DataFactory.Instance.Save(box, SaveAction.Publish, AccessLevel.NoAccess);
        }

        [Test]
        public void WhenSuggestionIsMade_ItsAdded_ToTheSuggestionBoxContainer()
        {
            SuggestionBox box = new SuggestionBox(boxLink, DataFactory.Instance);
            Suggestion original = new Suggestion("Make it stop", "I see no end to the possibilities"); 
            box.Submit(original);

            PageDataCollection stored = DataFactory.Instance.GetChildren(boxLink);

            Assert.That(stored[0].PageName, Is.EqualTo(original.Title));
            Assert.That(stored[0]["MainBody"], Is.EqualTo(original.Body));
        }
    }


    public class Suggestion
    {
        public Suggestion(string questionTitle, string questionBody)
        {
            Title = questionTitle;
            Body = questionBody;
        }

        public string Title { get; set; }
        public string Body { get; set; }
    }


    public class SuggestionBox
    {
        private const int SuggestionPageTypeID = 11;
        private readonly PageReference containerLink;
        private readonly DataFactory dataFactory;
        
        public SuggestionBox(PageReference containerLink, DataFactory dataFactory)
        {
            this.containerLink = containerLink;
            this.dataFactory = dataFactory;
        }

        public void Submit(Suggestion suggestion)
        {
            PageData page = dataFactory.GetDefaultPageData(containerLink, SuggestionPageTypeID);
            page.PageName = suggestion.Title;
            page["MainBody"] = suggestion.Body;
            dataFactory.Save(page, SaveAction.Publish, AccessLevel.NoAccess);
        }

        public Suggestion Dequeue()
        {
            throw new NotImplementedException();
        }
    }
}